package com.ruoyi.web.controller.okr;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.OkrCycle;
import com.ruoyi.system.domain.OkrInfo;
import com.ruoyi.system.domain.OkrTask;
import com.ruoyi.system.service.IOkrCycleService;
import com.ruoyi.system.service.IOkrInfoService;
import com.ruoyi.system.service.IOkrTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("/workbench")
@Controller
public class MyOkrWorkbench extends BaseController {
    private String prefix = "okr";

    @Autowired
    private IOkrTaskService okrTaskService;

    @Autowired
    private IOkrInfoService okrInfoService;

    @Autowired
    private IOkrCycleService cycleService;

    @RequestMapping("")
    public String getWorkbench(){
        return prefix + "/workbench";
    }

    @RequestMapping("/getTaskListToday")
    @ResponseBody
    public TableDataInfo getTaskListToday(OkrTask okrTask) throws ParseException {
        startPage();
        okrTask.setUserId(ShiroUtils.getUserId());
        List<OkrTask> list = okrTaskService.selectOkrTaskList(okrTask);
//        System.out.println(okrTasks);
        List<OkrTask> res = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        Date now = format.parse(DateUtils.getDate());
        for (OkrTask task : list){
            date = format.parse(task.getBeginTime());
            long dis = (now.getTime() - date.getTime()) / 24 / 60 / 60 / 1000;
            if (dis <= 3L  && dis > 0L){
                res.add(task);
            }
        }
        return getDataTable(res);
    }


    @RequestMapping("/getOKRList")
    @ResponseBody
    public TableDataInfo getOKRList(OkrInfo okrInfo){
        Long userId = ShiroUtils.getUserId();
        //如果没有选中OKR区间，默认使用当前再用的
        if (okrInfo.getCycleId() == null)
        {
            List<OkrCycle> useCycleList = cycleService.selectOkrUseCycleList();
            okrInfo.setCycleId(useCycleList.get(0).getId());  //默认使用的
        }
        List<OkrInfo> list = okrInfoService.selectOkrInfoListByUserId(okrInfo, userId);
        List<OkrInfo> res = new ArrayList<>();
        //挑出目标
        for (OkrInfo info : list){
            if (info.getRowType().equals(0L)){
                res.add(info);
            }
        }
        return getDataTable(res);
    }

    @RequestMapping("/getTaskListTodayForSeven")
    @ResponseBody
    public TableDataInfo getTaskListTodayForSeven(OkrTask okrTask) throws ParseException {
        startPage();
        okrTask.setUserId(ShiroUtils.getUserId());
        List<OkrTask> list = okrTaskService.selectOkrTaskList(okrTask);
        List<OkrTask> res = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        Date now = format.parse(DateUtils.getDate());
        for (OkrTask task : list){
            date = format.parse(task.getBeginTime());
            long dis = (now.getTime() - date.getTime()) / 24 / 60 / 60 / 1000;
            if (dis <= 7L  && dis > 0L){
                res.add(task);
            }
        }
        return getDataTable(res);
    }

    @RequestMapping("/getTaskStatus")
    @ResponseBody
    public TableDataInfo getTaskStatus(OkrTask okrTask){
        startPage();
        okrTask.setUserId(ShiroUtils.getUserId());
        List<OkrTask> list = okrTaskService.selectOkrTaskList(okrTask);
        List<OkrTask> res = new ArrayList<>();
        for (OkrTask task: list){
            if (task.getStatus() == 1){
                res.add(task);
            }
        }
        return getDataTable(res);
    }

    @RequestMapping("/getTaskListForEndThree")
    @ResponseBody
    public TableDataInfo getTaskListForEndThree(OkrTask okrTask) throws ParseException {
        startPage();
        okrTask.setUserId(ShiroUtils.getUserId());
        List<OkrTask> list = okrTaskService.selectOkrTaskList(okrTask);
        List<OkrTask> res = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        Date now = format.parse(DateUtils.getDate());
        for (OkrTask task : list){
            date = format.parse(task.getEndTime());
            long dis = (date.getTime() - now.getTime()) / 24 / 60 / 60 / 1000;
            if (dis <= 3L  && dis >= 0L){
                res.add(task);
            }
        }
        return getDataTable(res);
    }

    @RequestMapping("/getTaskListForEndSeven")
    @ResponseBody
    public TableDataInfo getTaskListForEndSeven(OkrTask okrTask) throws ParseException {
        startPage();
        okrTask.setUserId(ShiroUtils.getUserId());
        List<OkrTask> list = okrTaskService.selectOkrTaskList(okrTask);
        List<OkrTask> res = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        Date now = format.parse(DateUtils.getDate());
        for (OkrTask task : list){
            date = format.parse(task.getEndTime());
            long dis = (date.getTime() - now.getTime()) / 24 / 60 / 60 / 1000;
            if (dis <= 7L  && dis >= 0L){
                res.add(task);
            }
        }
        return getDataTable(res);
    }
}
